﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Security;
using System.Security.Principal;

using Happy.Web.Mvc.ExtJs;
using Happy.Web.Mvc.Security;
using Happy.Security.Application.Authentication;

namespace Happy.Security.Controllers.Authentication
{
    public sealed class AuthenticateController : ExtJsController
    {
        public ActionResult Login(string username, string password)
        {
            var authenticateResult = this.Service<AuthenticateService>().Authenticate(username, password);

            if (authenticateResult.IsAuthenticated)
            {
                HappyAuthorization.SetPrincipal(authenticateResult.Principal);
            }

            return this.NewtonsoftJson(new
            {
                success = true,
                isAuthenticated = authenticateResult.IsAuthenticated
            });
        }

        public ActionResult Current()
        {
            var isAuthenticated = this.HttpContext.User.Identity.IsAuthenticated;

            return this.NewtonsoftJson(new
            {
                success = true,
                isAuthenticated
            });
        }
    }
}
